var MapEditor = function (elementId, centerXY) {

    this.elementId = elementId;
    this.map = null;
    this.initMap(centerXY);
    MapEditor.instance = this;
}


MapEditor.instance = null;
var titleopts = [];
var titlecontent;
var tempobj = [];
var marker;

MapEditor.prototype.getwindow = function (setWidth, setHeight, setTitle) {

    titlecontent = {
        title: setTitle, //标题
        width: setWidth, //宽度
        height: setHeight, //高度
        panel: "panel",         //检索结果面板
        enableAutoPan: true,     //自动平移
        enableSendToPhone: false, //是否启动发送到手机功能
        searchTypes: []
    };
    titleopts.push(titlecontent);
    return titlecontent;
}
MapEditor.prototype.initMap = function (centerXY) {
    this.map = new BMap.Map(this.elementId);
    var poi = new BMap.Point(121.389257, 31.216542);
    this.map.centerAndZoom(poi, 14);
    this.map.enableScrollWheelZoom();
    this.map.setCurrentCity("上海");          // 设置地图显示的城市 此项是必须设置的
    this.map.enableScrollWheelZoom(true);
}

MapEditor.prototype.addCircle = function (centerXY) {
}


MapEditor.prototype.addMarker = function (obj, flag, digIcon) {

    var point = new BMap.Point(parseFloat(obj.lng), parseFloat(obj.lat));
    if (digIcon == null) {
        var myIcon = new BMap.Icon("../../../resources/images/park/" + obj.imagename + ".png", new BMap.Size(25, 39), {
            imageSize: new BMap.Size(20, 34)
        });
    } else {
        var myIcon = new BMap.Icon("../../../resources/images/park/" + obj.imagename + ".png", new BMap.Size(60, 60), {imageSize: new BMap.Size(30, 30)});
    }
    marker = new BMap.Marker(point, {icon: myIcon});
    marker.setAnimation(BMAP_ANIMATION_DROP); //跳动的动画
    obj.marker = marker;
    tempobj.push(obj);
    this.map.addOverlay(marker);
    if (flag == null || flag == true) {
        setlabel(marker, obj.label);
    }

    addClickHandlertwo(marker, this.map, obj.content, obj.title);
    // addClickHandler(obj.content, marker, obj, this.map, obj.title);
    return obj;

}

MapEditor.prototype.getContent = function (obj) {
    //var point =new BMap.Point(parseFloat(obj.lng), parseFloat(obj.lat));
    //var marker = new BMap.Marker(point);
    //this.map.addOverlay(marker);
    //return point;
}
MapEditor.prototype.linkMap = function (obj, marker) {
    this.map.centerAndZoom(obj.point, 15);
    /*this.map.openInfoWindow(obj.infoWindow, obj.point); //开启信息窗口*/
    var searchInfoWindow = null;
    var searchInfoWindow = new BMapLib.SearchInfoWindow(this.map, obj.content, obj.titlecontent);
    searchInfoWindow.open(marker);

}
function setlabel(marker, labelcontent) {
    var label = new BMap.Label(labelcontent, {offset: new BMap.Size(20, -10)});
    label.setStyle({
        color: "blue",
        fontSize: "15px",
        height: "20px",
        lineHeight: "20px",

        fontFamily: "微软雅黑"
    });
    marker.setLabel(label);
}
MapEditor.prototype.addRouteLine = function () {

}
MapEditor.prototype.removeOverlay = function (overlayid) {

}
MapEditor.prototype.removeAll = function () {
    this.map.clearOverlays();

}
function addClickHandlertwo(marker, map, content, title) {
    marker.addEventListener("click", function (e) {
        openInfotwo(marker, map, content, title)
    });

}
function openInfotwo(marker, map, content, title) {
    var searchInfoWindow = null;
    var searchInfoWindow = new BMapLib.SearchInfoWindow(map, content, title);
    searchInfoWindow.open(marker);

}
function openInfo(content, e, obj, map, title) {
    //  var p = e.target;
    var point = new BMap.Point(obj.lng, obj.lat);
    var infoWindow = new BMap.InfoWindow(content, title);  // 创建信息窗口对象
    map.openInfoWindow(infoWindow, point); //开启信息窗口
}
function addClickHandler(content, marker, obj, map, title) {
    marker.addEventListener("click", function (e) {
            openInfo(content, e, obj, map, title)
        }
    );
}

